package com.resLoader
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;

	import com.utils.Print;

	/**
	 * 加载的基类
	 * @author hyy
	 *
	 */
	public class Resource
	{
		public var resData : ResData;

		public var isLoading : Boolean = false;

		/**
		 *优先级别
		 */
		public var priority : int;

		/**
		 * 加载地址
		 */
		public var url : String;

		/**
		 * 名称
		 */
		public var name : String;

		/**
		 * 当前重复加载次数
		 */
		protected var currCount : int = 0;

		/**
		 *  最大重复加载次数
		 */
		protected const MAX_RELOAD : int = 5;

		/**
		 * 加载成功后回调
		 */
		protected var completeFun : Function;

		/**
		 * 报错后回调
		 */
		protected var errorFun : Function;

		/**
		 * 进度条
		 */
		protected var onProgress : Function;

		/**
		 * 加载地址
		 */
		protected var request : URLRequest;

		/**
		 * 总共下载大小
		 * 默认是1000k
		 */
		public var bytesTotal : uint = 300 * 1000;

		/**
		 * 当前加载大小
		 */
		public var bytesLoaded : uint;

		protected var context : LoaderContext;

		public function Resource(res_url : String, returnFun : Function, errorFun : Function = null, onProgress : Function = null)
		{
			this.url = res_url;

//			if(Version.version)
//			{
//				res_url= Version.convertURL(res_url);
//			}
//			else
//			{
//				res_url = res_url + "?v="+Math.random();
//			}
//
//			res_url = Config.webRoot + res_url;

//			this.request = new URLRequest(encodeURI(res_url));
			this.request = new URLRequest(res_url);
			this.completeFun = returnFun;
			this.errorFun = errorFun;
			this.onProgress = onProgress;
		}

		/**
		 * 开始加载,需要手动调用load
		 *
		 */
		public function load(context : LoaderContext = null) : void
		{
			this.context = context;
			this.isLoading = true;
		}

		/**
		 * 清除监听
		 *
		 */
		public function cleanListeners() : void
		{

		}

		/**
		 * 加载进度条
		 * @param evt
		 *
		 */
		protected function onProgressEvent(evt : ProgressEvent) : void
		{
			if (evt.bytesTotal > 0)
				bytesTotal = evt.bytesTotal;
			bytesLoaded = evt.bytesLoaded;

			if (onProgress != null)
			{
				onProgress(this);
			}
		}

		protected function onDownloadError(evt : IOErrorEvent) : void
		{
			onFailed(evt.text);
		}

		protected function onDownloadSecurityError(evt : SecurityErrorEvent) : void
		{
			onFailed(evt.text);
		}

		/**
		 * 加载完成
		 * @param evt
		 *
		 */
		protected function onDownLoadComplete(evt : Event) : void
		{
			cleanListeners();

			if (completeFun != null)
			{
				completeFun(this);
				completeFun = null;
			}
		}

		/**
		 * 加载异常输出
		 * @param msg
		 *
		 */
		protected function onFailed(msg : String) : void
		{
			//3次尝试重新加载
			if (currCount < MAX_RELOAD)
			{
				currCount++;
				load(context);
				Print.error("尝试重新加载: ", currCount, request.url);
				return;
			}
			Print.error("加载错误: ", this.url);
			cleanListeners();

			if (errorFun != null)
			{
				errorFun(this);
				errorFun = null;
			}
		}

		/**
		 * 停止下载
		 *
		 */
		public function stop() : void
		{
			cleanListeners();
		}

		/**
		 * 销毁
		 *
		 */
		public function destory() : void
		{
			stop();
			cleanListeners();
			request = null;
			completeFun = null;
			errorFun = null;
			onProgress = null;
			context = null;

			if (resData)
			{
				resData.destroy();
				resData = null;
			}
		}

	}
}